package net.cn_su.sharpeyes.utils.http;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.util.Map;

/**
 * jackson转换
 * @author 谌毅
 *
 */
public class JacksonMapper {

	private static ObjectMapper mapper = null;
	static{
		mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}
	
	/**
	 * 获取直接对象
	 * @return
	 */
    public static ObjectMapper getMapper() {
		return mapper;
	}
    
    /**
	 * 解析成Map
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> parse(String jsonStr) {
		Map<String, Object> result = null;
		try {
			result = (Map<String, Object>)mapper.readValue(jsonStr, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 解析成指定对象
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T parse(String jsonStr, Class<?> T) {
		T result = null;
		try {
			result = (T)mapper.readValue(jsonStr, T);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 解析成指定对象，带泛型
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T parseToList(String jsonStr, TypeReference<T> reference) {
		T result = null;
		try {
			result = (T)mapper.readValue(jsonStr, reference);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
